import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container-scroll'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const CrisisContactTable	= $Lazy.state(()=>import('./cpt/crisis-contact-table'))
const SpecimenCollectionTable	= $Lazy.state(()=>import('./cpt/specimen-collection-table'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== Lazy
const width = '25%'
const boolData = [{ name:'是', value: '1' }, { name:'否', value:'0' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '医院编码',			name: 'hosp_code', 					width, required:true, title:'医院基础信息', disabled:true },
			{ label: '医院条码',			name: 'hosp_spec', 					width, required:true },
			{ label: '医院全称',			name: 'hosp_name', 					width, required:true },
			{ label: '医院别称',			name: 'hosp_other_name', 			width },
			{ label: '英文名称',			name: 'hosp_english_name', 			width },
			{ label: '医院级别',			name: 'level', 						width, type:'select', loading:true, idStr:'code',	value:'39001', onChange: () => {
				this.state?.edit === '1' && message.then(f=>f.warn('此操作会影响医院的项目价格', 300))
			} },
			{ label: '医院性质',			name: 'nature', 					width, required:true,  type:'select', loading:true, idStr:'code' },
			{ label: '医院电话',			name: 'hospital_phone', 			width },
			{ label: '医院传真',			name: 'fax', 						width },
			{ label: '法人代表',			name: 'legal_person', 				width },
			{ label: '社会统一信用代码',	name: 'social_credit_code', 		width, required:true },
			{ label: '银行账号',			name: 'bank_account', 				width, br:true },
			{ label: '银行账号名称',		name: 'bank_account_name', 			width},
			{ label: '开户行',			name: 'opening_bank', 				width },
			{ label: '开户行地址',		name: 'opening_address', 			width },
			{ label: '开票单位名称',		name: 'organization_name', 			width, required:true },
			{ label: 'NC单位',			name: 'nc_name', 					width },
			{ label: 'NC单位编码',		name: 'organization_code', 			width },
			{ label: '医院联系人',		name: 'hospital_contacts', 			width },
			{ label: '联系人电话',		name: 'telephone', 					width },
			{ label: '医院地址信息',		name: 'areaData', 					half:true,	type:'address', onRef: ref=> this.address = ref },
			{ label: '医院详细地址',		name: 'address', 					half:true },
			{ label: '医院 logo',			name: 'logo', 					full:true, type:'upload', code:131 },
			{ type:'cpt', component: ()=> <CrisisContactTable onRef={ ref => this.crisisContactTableRef = ref } disabled={!this.state.edit} />, full:true },
			// 业务信息
			{ label: '业务区域',			name: 'region', 					half:true, 	required:true, title:'业务信息', type:'select', loading:true, 
				onValue: ({ value,formData })=>{
					this.formRef.reset(['regional_super_id', 'regional_manager_id', 'salesman'])
					if(value){
						// 增加获取区域经理
						$http.pull(null,'bs-salesman/getLeaderUser', { param:{ area_id:value, key: 'me' }, loading:true }).then( items =>{
							if($fn.hasArray(items)){
								const value = items[0].uuid
								this.formRef.setValue({ regional_super_id:value })
							}
						})
						//增加大区域经理接口
						$http.pull(null,'bs-salesman/getLeaderUser', { param:{ area_id:value, key: 'parent' }, loading:true }).then( items =>{
							if($fn.hasArray(items)){
								const value = items[0].uuid
								this.formRef.setValue({ regional_manager_id:value })
							}
						})
						$http.pull(null,'bs-salesman/select', { param:{ area_id:value }, loading:true }).then( items =>{
							if($fn.hasArray(items)){
								// const value = items[items.length - 1].value
								// this.formRef.setValue({ regional_super_id:value, regional_manager_id:value })
								$fn.setRow(formData, 'salesman', items)
								this.setState({ formData })
							}
						})
						this.submit.area_id = value
					}
				}
			},
			{ label: '客户等级',			name: 'customer_level', 			width, 		type:'select', loading:true, idStr:'code' },
			{ label: '区域经理',			name: 'regional_super_id', 			width, 		type:'select', loading:true, br:true, disabled:true, p:'请先选择业务区域' },
			{ label: '大区域经理',		name: 'regional_manager_id', 		width, 		required:true, type:'select', disabled:true, loading:true,  },
			{ label: '业务员',			name: 'salesman', 					half:true, 	required:true, type:'select', mode:'multiple', },
			{ label: '回款方式',			name: 'back_money', 				width, 		type:'select', loading:true, idStr:'code' },
			{ label: '结算单打印要求',	name: 'printing', 					full:true,  type:'textarea' },
			{ label: '特别注意事项',		name: 'need_matter', 				full:true,  type:'textarea' },
			// 业务信息	
			{ label: '标本收取人',		name: 'collector', 					half:true, 	required:true, type:'select', loading:true, mode:'multiple', title:'物流信息' },
			{ label: '物流区域主管',		name: 'logistics_area_super_id',	half:true, 	required:true, type:'select', loading:true },
			{ type:'cpt', component: ()=> <SpecimenCollectionTable onRef={ ref => this.specimenCollectionTableRef = ref } disabled={!this.state.edit} />, full:true },
			// 客户报告单要求信息
			{ label: '报告单大小要求',	name: 'report_size', 				width,		required:true, type:'select', loading:true, idStr:'code', title:'客户报告单要求信息' },
			{ label: '报告单主标题',		name: 'report_rise', 				width },
			{ label: '报告单副标题',		name: 'custom_report_rise', 		width },
			{ label: '报告单二级标题',	name: 'custom_report_rise_sub', 	width },
			{ label: '报告单是否加盖印章',name: 'is_stamp', 					width, type:'radio', data:boolData, value:'0' },
			{ label: '报告单是否显示网页',name: 'is_display_web', 			width, type:'radio', data:boolData, value:'0'  },
			{ label: '是否显示客服热线',	name: 'is_cust_service_hotline', 	width, type:'radio', data:boolData, value:'0'  },
			{ label: '物流是否配送',		name: 'is_delivery', 				width, type:'radio', data:boolData, value:'0'  },
			{ label: '是否显示送检单位',	name: 'is_show_hosp', 				width, type:'radio', data:boolData, value:'1'  },
			{ label: '客服热线',			name: 'service_hotline', 			width },
			{ label: '报告单配送份数',	name: 'custom_report_rise_sub', 	width, type:'number' },
			{ label: '公章',				name: 'official_seal', 				full:true, type:'upload', code:131 },
			{ label: '报告单检测方地址',	name: 'custom_report_rise_path', 	full:true, type:'textarea' },
			{ label: '病人资料特殊要求',	name: 'patient_special_request', 	full:true, type:'textarea' },
			{ label: '检测项目特殊要求',	name: 'project_special_request', 	full:true, type:'textarea' },
			{ label: '客户其他特殊要求',	name: 'customer_special_request', 	full:true, type:'textarea' },
		],
	}
	
	componentDidMount(){
		const { uuid, edit } = $fn.query()
		
		if(uuid){ this.uuid = uuid }
		
		const { formData } = this.state
		
		$fn.disabledRows(formData,'hosp_code', true)
		
		this.setState({ edit })
		
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			const dic = await f.getDicCache({ code:[39000, 44100, 45100, 45200, 38000] })
			// 医院级别
			$fn.setRow(formData, 'level', dic['39000'])
			// 医院性质
			$fn.setRow(formData, 'nature', dic['44100'])
			// 客户等级
			$fn.setRow(formData, 'customer_level', dic['45100'])
			// 回款方式
			$fn.setRow(formData, 'back_money', dic['45200'])
			// 报告单大小要求
			$fn.setRow(formData, 'report_size', dic['38000'])
			// 业务区域
			$fn.setRow(formData, 'region', await f.getCache({ api:'bs_area' }))
			// 区域经理
			// $fn.setRow(formData, 'regional_super_id', await f.getCache({ api:'salesman', param:{ area_id:value, key: 'me' } }))
			// 大区域经理
			// $fn.setRow(formData, 'regional_manager_id', await f.getCache({ api:'salesman', param:{ area_id:value, key: 'parent' } }))
			
			const employee = await f.getCache({ api:'employee' })
			// 标本收取人
			$fn.setRow(formData, 'collector', employee)
			// 物流区域主管
			$fn.setRow(formData, 'logistics_area_super_id', employee)
			//
			this.setState({ formData })
		})
	}
	
	fetch = async () => {
		const { formData } = this.state
		if(!this.uuid){
			const { uuid } =  await $http.submit(null,'bs-hospital/add', { isPull:true })
			this.uuid = uuid
		}
		
		if(this.uuid){
			$http.submit(this, 'bs-hospital/info', { param:{ uuid:this.uuid }, isPull:true }).then( async data=>{
				const { 
					uuid, get_type, add_status, enabled, get_date_type, get_number,
					province_id, city_id, area_id, report_size,
					hosp_code, hosp_spec, region,
				} = data
				
				const { edit } = this.state
				
				setTimeout(()=>{
					// 危急值联络人
					this.crisisContactTableRef?.load(uuid)
					// 标本收取地点
					this.specimenCollectionTableRef?.load(uuid)
				},50)
				this.submit = { uuid, get_type, add_status, enabled, get_date_type, get_number }
				if( +edit !== 2){
					let areaData
					if(province_id){
						areaData = [ province_id, city_id, area_id ]
						await this.address?.load?.( areaData )
					}
					// 增加获取区域经理
					$http.pull(null,'bs-salesman/getLeaderUser', { param:{ area_id: region, key: 'me' }, loading:true }).then( items =>{
						if($fn.hasArray(items)){
							const data = items.map(v => ({...v, value: v.uuid, name: v.real_name}))
							$fn.setRow(formData, 'regional_super_id', data)
							this.setState({ formData })
						}
					})
					//增加大区域经理接口
					$http.pull(null,'bs-salesman/getLeaderUser', { param:{ area_id: region, key: 'parent' }, loading:true }).then( items =>{
						if($fn.hasArray(items)){
							const data = items.map(v => ({...v, value: v.uuid, name: v.real_name}))
							$fn.setRow(formData, 'regional_manager_id', data)
							this.setState({ formData })
						}
					})
					$http.pull(null,'bs-salesman/select', { param:{ area_id:region }, loading:true }).then( items =>{
						if($fn.hasArray(items)){
							// const value = items[items.length - 1].value
							// this.formRef.setValue({ regional_super_id:value, regional_manager_id:value })
							$fn.setRow(formData, 'salesman', items)
							this.setState({ formData })
						}
					})
					this.formRef.setValue({...data, report_size: report_size || '38004', areaData})
				}else{
					this.formRef.setValue({ hosp_code, hosp_spec, report_size: report_size || '38004'})
				}
			})
		}
	}
	
	// 操作按钮
	controls = () => [
		this.state.edit && Controls.click.call(this,'保存', ()=>{ 
			this.setState({ save:1 }, () => this.formRef.submit() )
		}, { code:'Enter' }),
		this.state.edit && Controls.click.call(this,'保存并审核', ()=>{ 
			this.setState({ save: 0 }, () => this.formRef.submit() )
		}),
		Controls.back.call(this)
	]
	
	render(){
		const { save, edit } = this.state
		let api = 'bs-hospital/edit'
		let msg = '保存'
		if(save === 0){
			api = 'bs-hospital/save'
			msg = '保存并审核'
		}
		let txt = '查看'
		if( +edit ===1 ){
			txt = '医院客户编辑'
		}else if( +edit ===2 ){
			txt = '医院客户新增'
		}
		
		return (
			<Page title={txt} className='p10' controls={this.controls()}>
				{ SubmitForm.call(this,{ labelWidth:'s11', msg,  api, disabled: !edit, onForm: this.fetch, backRefresh:true, onComplete:()=>{
					$fn.dataRemove('bs-hospital/select')
				} }) }
			</Page>
		)
	}
}

export default Index